﻿using System;
using System.Collections.Generic;
using System.Text;
using TCPSockets;
using TCPSockets.ServerEvents.Handlers;
using TCPSockets.ServerEvents.Args;
using Galaxy.Portal.TCP;
using Galaxy.ModuleInterface;
using Galaxy.ModuleInterface.Events;
using Galaxy.ModuleInterface.Messages;
using System.Net;
using Galaxy.Portal.Messages;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System.Threading;

namespace Galaxy.Portal
{                                                
    public sealed class Program
    {
        private static List<IGalaxyModuleMessage> sf_MessagesList = new List<IGalaxyModuleMessage>();


        [Serializable]
        private class TestClass
        {
            int qwe;
            string asd;

            public TestClass(int qwe, string asd) { this.qwe = qwe; this.asd = asd; }
        }

        public static void Main(string[] args)
        {
            //TestServer();
            CTCPPortal Portal1, Portal2;
            Portal1 = new CTCPPortal();
            Portal2 = new CTCPPortal();

            CTCPServer Server = new CTCPServer(8888);
            Server.OnReceiveData += new OnReceivedDataEventHandler(Server_OnReceiveData);
            //Server.Enabeld = true;

            Portal1.Port = 8888;
            Portal2.Port = 8889;

            Portal1.Initialize();
            Portal2.Initialize();

            Portal1.NewMessage += Portal1_NewMessage;

            Portal1.Start();
            Portal2.Start();

            byte[] lv_SendBuffer = new byte[1024];

            for(int i = 0; i < lv_SendBuffer.Length; i++) lv_SendBuffer[i] = 7;

            MemoryStream lv_TempStream = new MemoryStream(1024 * 1024 * 100);

            for(int i = 0; i < 1024 * 100; i++) lv_TempStream.Write(lv_SendBuffer, 0, 1024);

            CTypedMessage<byte[]> NetMessage = new CTypedMessage<byte[]>(lv_TempStream.ToArray(), Portal2);
            NetMessage.Destination = new CNetDestination(IPAddress.Parse("127.0.0.1"), 8888,
                Portal1);

            Console.WriteLine(" > " + NetMessage + " " + NetMessage.Data.Length);

            for(int i = 0; i < 10; i++)
            {
                Portal2.SendMessage(NetMessage);
                Console.ReadLine();
            }

            Console.ReadLine();

            Console.WriteLine("Ошиб" + (sf_MessagesList.TrueForAll(delegate(IGalaxyModuleMessage Message)
              {
                  return ((CTypedMessage<byte[]>)Message).Data.Length ==
                      ((CTypedMessage<byte[]>)NetMessage).Data.Length;
              }) ? "ок нет" : "ки есть"));



            Console.WriteLine("Работу закончил");
            Console.ReadLine();
        }

        static void Server_OnReceiveData(CTCPServer Server, DataEventArgs Args)
        {
            Console.WriteLine(Args.Data.Length);
        }

        private static void Portal1_NewMessage(IGalaxyModule Module, NewMessageEventArgs Arg)
        {
            IGalaxyModuleMessage Message = Arg.Message;
            if(Message != null)
                Console.Write((sf_MessagesList.Count + 1).ToString() + ":" + Message + " ");
            if(Message is CTypedMessage<byte[]>)
                Console.WriteLine(((CTypedMessage<byte[]>)Message).Data.Length);
            sf_MessagesList.Add(Arg.Message);
        }

        private static void TestServer()
        {
            CTCPServer lv_Server = new CTCPServer(8080);
            CTCPClient lv_Client = new CTCPClient("127.0.0.1", 8080);

            object lv_TestObject = new TestClass(123, "qwe");

            lv_Server.OnClientConnected += delegate { Console.WriteLine("CLient connected"); };
            lv_Server.Enabeld = true;

            lv_Client.Connected = true;
            lv_Client.SendObject(lv_TestObject);



            lv_Server.Enabeld = false;
        }
    }
}
